package cc.rengu.igas.bsps.core.service.trans;


import cc.rengu.igas.bsps.common.dao.BspsMchntBindInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsTermInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBindInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsTermInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntBindInfo;
import cc.rengu.igas.bsps.common.entity.TermInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.TermInfoListBean;
import cc.rengu.igas.bsps.facade.request.MchntTermQueryRequest;
import cc.rengu.igas.bsps.facade.response.MchntTermQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020.10.14
 * 终端查询
 */
public class MchntTermQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntTermQueryRequest mchntTermQueryRequest = new MchntTermQueryRequest();
        ConvertUtil.convertOutput(mchntTermQueryRequest);
        return mchntTermQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        MchntTermQueryRequest mchntTermQueryRequest = (MchntTermQueryRequest) request;
        String instId = mchntTermQueryRequest.getHeader().getInstId();
        //商户号
        String mchntNo = mchntTermQueryRequest.getMchntNo();
        //终端号
        String termNo = mchntTermQueryRequest.getTermNo();
        //用户标识
        String userId = mchntTermQueryRequest.getUserId();

        if (StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(instId)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        //商户绑定用户关系
        BspsMchntBindInfoMapper mchntBindInfoMapper = new BspsMchntBindInfoMapperImpl();
        MchntBindInfo mchntBindInfo = mchntBindInfoMapper.selectBspsMchntBindInfoListByUserIdAndMchntNo(instId, userId, mchntNo);
        if (mchntBindInfo == null) {
            rglog.error("商户绑定关系不存在");
            throw new BizException(RespCodeEnum.GET_USER_BIND_INFO_ERROR.getRespCode(), RespCodeEnum.GET_USER_BIND_INFO_ERROR.getRespDesc());
        }
        //商户基础信息
        List<TermInfoListBean> termInfoList = new ArrayList<>();
        MchntTermQueryResponse mchntTermQueryResponse = new MchntTermQueryResponse();
        BspsTermInfoMapper bspsTermInfoMapper = new BspsTermInfoMapperImpl();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(mchntTermQueryRequest.getPageIndex());
        pageInfo.setPageSize(mchntTermQueryRequest.getPageSize());
        PageResult pageResult = bspsTermInfoMapper.selectBspsTermInfoListByMchntNoAndTermNo(instId, mchntNo, termNo, pageInfo);
        if (pageResult == null || pageResult.getResult().isEmpty()) {
            rglog.info("根据查询条件，数据为0条!");
            mchntTermQueryResponse.setTotalPage(0);
            mchntTermQueryResponse.setTotalRows(0);
        } else {
            for (Object object : pageResult.getResult()) {
                TermInfo termInfo = (TermInfo) object;
                TermInfoListBean termInfoListBean = new TermInfoListBean();
                termInfoListBean.setAuditStatus(termInfo.getAuditStatus());
                termInfoListBean.setContactName(termInfo.getContactName());
                termInfoListBean.setContactPhone(termInfo.getContactPhone());
                termInfoListBean.setMchntNo(termInfo.getMchntNo());
                termInfoListBean.setModelName(termInfo.getModelCode());
                termInfoListBean.setTermNo(termInfo.getTermNo());
                termInfoListBean.setTermStatus(termInfo.getTermStatus());
                termInfoList.add(termInfoListBean);
            }
            mchntTermQueryResponse.setTermInfoList(termInfoList);
            mchntTermQueryResponse.setPageIndex(pageResult.getPageInfo().getPageIndex());
            mchntTermQueryResponse.setPageSize(pageResult.getPageInfo().getPageSize());
            mchntTermQueryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
            mchntTermQueryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
        }
        /* 重组返回结果 */
        BizResponse<MchntTermQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntTermQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntTermQueryResponse mchntTermQueryResponse = (MchntTermQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntTermQueryResponse);
    }


}
